﻿// Favorites Task Pane Word Add-In - Attribution notice
// Author(s):
//
//      Stacy Draper (stacyDraper@WildWires.com, http://www.WildWires.com)
//
// This file is distributed under the terms of the Microsoft Community 
// License (Ms-CL). See http://www.codeplex.com/features/Project/License.aspx
// for the complete terms of use. 
// 
// For any distribution that contains code from this file, this notice of 
// attribution must remain intact, and a copy of the license must be 
// provided to the recipient.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using System.IO;



namespace SharePointNation
{
    partial class Favorites : UserControl
    {
        ContextMenuStrip contextMenuStrip = null;
        Point p;
        Image image;
        public Favorites()
        {
            Microsoft.Office.Tools.Ribbon.RibbonButton rb = new Microsoft.Office.Tools.Ribbon.RibbonButton();
            
            rb.OfficeImageId = "AccessRefreshAllLists";
            image = rb.Image;

            //Copy

            InitializeComponent();
            addFavorites();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void addFavorites()
        {
            treeView1.Nodes.Clear();
            
            TreeNode treeNodeFavorites = new TreeNode("Favorites");

            ToolStripMenuItem contextMenuItem;
            contextMenuStrip = new ContextMenuStrip();
            contextMenuItem = new ToolStripMenuItem("Refresh");
            contextMenuItem.Image = image;
            contextMenuItem.Click += new System.EventHandler(this.contextMenuItem_Click);
            contextMenuItem.MouseLeave += new EventHandler(contextMenuItem_MouseLeave);

            contextMenuStrip.Items.Add(contextMenuItem);

            treeNodeFavorites.ContextMenuStrip = contextMenuStrip;

            treeView1.Nodes.Add(treeNodeFavorites);

            TreeNode node = treeView1.Nodes[0];

            String favoritesPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\favorites";
            DirectoryInfo di = new DirectoryInfo(favoritesPath);
            itterateFavoritesDirectories(di, node);
        }



        private void itterateFavoritesDirectories(DirectoryInfo di, TreeNode node)
        {
            DirectoryInfo[] favoritesDirectories = di.GetDirectories();
            for (int i = 0; i < favoritesDirectories.Length; i++)
            {
                TreeNode newNode = node.Nodes.Add(favoritesDirectories[i].Name);
                getFiles(favoritesDirectories[i].GetFiles("*.url"), newNode);
                itterateFavoritesDirectories(favoritesDirectories[i], newNode);
            }
        }

        private void getFiles(FileInfo[] fi, TreeNode node)
        {
            for (int i = 0; i < fi.Length; i++)
            {
                node.Nodes.Add(makeFileNode(fi[i]));
            }

        }

        private TreeNode makeFileNode(FileInfo fi)
        {
            TreeNode node = new TreeNode(fi.Name.Replace(fi.Extension, ""));
            node.Tag = fi;

            return node;
        }

        private void Favorites_Load(object sender, EventArgs e)
        {

        }

        private void insertText(string text)
        {


            Word.Application doc = favorites.Globals.ThisAddIn.Application;
            Word.Selection currentSelection = doc.Application.Selection;
            
            // Store the user's current Overtype selection
            bool userOvertype = doc.Options.Overtype;

            // Make sure Overtype is turned off.
            if (doc.Options.Overtype)
            {
                doc.Options.Overtype = false;
            }

            // Test to see if selection is an insertion point.
            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
            {
                currentSelection.TypeText(text);
                currentSelection.TypeParagraph();
            }
            else
                if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
                {
                    // Move to start of selection.
                    if (doc.Options.ReplaceSelection)
                    {
                        object direction = Word.WdCollapseDirection.wdCollapseStart;
                        currentSelection.Collapse(ref direction);
                    }
                    currentSelection.TypeText(text);
                    currentSelection.TypeParagraph();
                }
                else
                {
                    // Do nothing.
                }

            // Restore the user's Overtype selection
            doc.Options.Overtype = userOvertype;
        }

        private void contextMenuItem_Click(object sender, EventArgs e)
        {
            addFavorites();
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            p = treeView1.PointToClient(Cursor.Position);
            TreeNode node = treeView1.GetNodeAt(p);
            if (node != null)
            {
                if (node.Tag != null)
                {
                    treeView1.SelectedNode = node;

                    FileInfo fi = (FileInfo)node.Tag;
                    AMS.Profile.Ini ini = new AMS.Profile.Ini(fi.FullName);
                    String url = ini.GetValue("InternetShortcut", "URL", "");

                    System.Diagnostics.Process.Start(url);
                }
            }
        }

        private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                p = new Point(e.X, e.Y);
                TreeNode node = treeView1.GetNodeAt(p);
                if (node != null)
                {
                    if (node.Tag != null)
                    {
                        treeView1.SelectedNode = node;
                        mnuUrl.Show(treeView1, p);
                    }
                }
            }
        }

        private void ToolStrip_MouseLeave(object sender, EventArgs e)
        {
            mnuUrl.Hide();
        }

        void contextMenuItem_MouseLeave(object sender, EventArgs e)
        {
            contextMenuStrip.Hide();
        }

        
        private void ContextMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.GetNodeAt(p);
            if (node != null)
            {
                FileInfo fi = (FileInfo)node.Tag;
                AMS.Profile.Ini ini = new AMS.Profile.Ini(fi.FullName);
                String url = ini.GetValue("InternetShortcut", "URL", "");

                switch (((ToolStripMenuItem)sender).Name)
                {
                    case "ctxOpen":
                        System.Diagnostics.Process.Start(url);
                        break;
                    case "ctxInsert":
                        insertText(url);
                        break;
                    case "ctxCopy":
                        Clipboard.SetText(url);
                        break;

                    default:
                        break;
                }
            }
        }




    }
}
